package com.lyhlmj.oop_client.Frames.PlayFrame.Panels;

import com.lyhlmj.oop_client.ClientOperation.ClientUtil.Client;
import com.lyhlmj.oop_client.Style.Buttons.GeneralButton;
import com.lyhlmj.oop_client.Threads.GetChatInfoTask;
import com.lyhlmj.oop_client.Style.TextFields.MsgBox;
import com.lyhlmj.oop_client.Utils.GetScreenSize;
import org.json.JSONObject;

import java.awt.*;

import javax.swing.*;

//聊天室
public class ChatRoomPanel extends JPanel {
    private MsgBox msgBox = new MsgBox(); // 消息列表控件
    private JTextArea sendField = new JTextArea(); // 消息输入框
    private GeneralButton sendButton = new GeneralButton("发送 "); // 发送按钮
    private JTextArea currentUser = new JTextArea("当前用户：" + Client.client.getUsername());

    private Window owner;
    private Client client = Client.client;
    private GetChatInfoTask getChatInfoTask;

    //  得到Frame的大小
    int width = GetScreenSize.getFitSize()[0];
    int height = GetScreenSize.getFitSize()[1];

    public ChatRoomPanel(Window owner) {
        this.setOpaque(false);
        this.setPreferredSize(new Dimension(700, 500));
        this.setLayout(new BorderLayout());
        this.setBorder(BorderFactory.createEmptyBorder(0, 0, (int) (0.03 * height), (int) (0.05 * height)));//    添加空白边框
        this.owner = owner;
//        用来初始化当前界面
        initial();
    }

    private void initial() {
//         消息列表（可滚动）
        currentUser.setEnabled(true);
        this.add(currentUser, BorderLayout.NORTH);
        this.add(new JScrollPane(msgBox), BorderLayout.CENTER);

//        添加底部部件（发送按钮和消息输入框）
        JPanel bottom = new JPanel();
        bottom.setPreferredSize(new Dimension(200, 200));
        bottom.setLayout(new BorderLayout());
//        添加消息输入框
        bottom.add(new JScrollPane(sendField), BorderLayout.CENTER);
//        添加发送按钮
        bottom.add(sendButton, BorderLayout.SOUTH);
//        将bottom放入到chatPanel中
        this.add(bottom, BorderLayout.PAGE_END);

        // 启动消息接收线程 轮询
        //TODO 调整当前语句位置
        client.getChatInfoTask = new GetChatInfoTask(this);
        this.getChatInfoTask = client.getChatInfoTask;
        this.getChatInfoTask.start();

        welcome();  //添加欢迎文字
//        为按钮添加功能
        sendButton.addActionListener((e) -> {
//            单击按钮执行下列方法
            onSendMessage();
        });
    }

    public void onSendMessage() {
//        获取文本框内容
        String text = sendField.getText();

//        定义json对象
        JSONObject jsonReq = new JSONObject();
        JSONObject args = new JSONObject();
        args.put("from", client.getUsername());  //添加用户名
//        args.put("to", getOtherUser(client.getUsername()));  // 发送方的用户名为动态取值
        args.put("to", "limeijie");  // 发送方的用户名为动态取值
        args.put("content", text);  //添加文本框内容

        jsonReq.put("cmd", "sendMessage");
        jsonReq.put("args", args);

        client.send(jsonReq, new Client.Callback() {
            @Override
            public void onResult(int status, String reason, JSONObject data) {
                if (status != 0) {
                    JOptionPane.showMessageDialog(owner, reason);
                    return;
                }

                sendField.setText("");
            }
        });
    }

    // 添试添加几条数据
    private void welcome() {
        JSONObject msg = new JSONObject();
        msg.put("name", "系统");
        msg.put("time", "");
        msg.put("content", "还没有成员发送消息哦~");
        msgBox.appendMessage(msg);
    }

    public GetChatInfoTask getChatRetrieveTask() {
        return getChatInfoTask;
    }

    public MsgBox getMsgBox() {
        return msgBox;
    }

    private String getOtherUser(String username) {
        if (client.getPlayers()[0].equals(username)) {
            return client.getPlayers()[1];
        } else {
            return client.getPlayers()[0];
        }
    }
}
